package com.ruoyi.app.controller.bigScreen;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.app.domain.*;
import com.ruoyi.app.mapper.AppActivityRegMapper;
import com.ruoyi.app.mapper.AppCreditRulesMapper;
import com.ruoyi.app.service.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/system/big-screen")
public class BigScreenController extends BaseController {

    @Autowired
    private IAppStudentService studentService;
    @Autowired
    private IAppTeacherService teacherService;
    @Autowired
    private IAppActivityService activityService;
    @Autowired
    private IAppActivityRegService activityRegService;

    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private AppCreditRulesMapper creditRulesMapper;
    @Autowired
    private AppActivityRegMapper appActivityRegMapper;

    @Autowired
    private IAppCreditRulesService creditRulesService;
    @GetMapping("/counts")
    public AjaxResult getCounts() {
        MPJLambdaWrapper<AppActivityReg>lwq = new MPJLambdaWrapper<AppActivityReg>()
                .selectAll(AppActivityReg.class)
                .select(AppCreditRules::getPoints)
                .leftJoin(AppActivity.class,AppActivity::getActivityId,AppActivityReg::getActivityId)
                .leftJoin(AppCreditRules.class,AppCreditRules::getRuleId,AppActivity::getRuleId)
                ;
        List<AppActivityReg> dtos = appActivityRegMapper.selectJoinList(AppActivityReg.class,lwq);
        Map<String, Object> counts = new HashMap<>();
        counts.put("studentCount", studentService.selectAppStudentList(new AppStudent()).size());
        counts.put("teacherCount", teacherService.selectAppTeacherList(new AppTeacher()).size());
        counts.put("activityCount", activityService.selectAppActivityList(new AppActivity()).size());
        counts.put("creditCount", dtos.stream().map(x->x.getPoints()).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
        return AjaxResult.success(counts);
    }

    @GetMapping("/activity-types")
    public AjaxResult getActivityTypes() {
        List<Map<String, Object>> typeData = new ArrayList<>();
        List<SysDictData> sysDictData = dictTypeService.selectDictDataByType("app_activity_category");
        for (SysDictData sysDictDatum : sysDictData) {
            Map<String, Object> data = new HashMap<>();

            AppActivity query = new AppActivity();
            query.setCategory(sysDictDatum.getDictValue());
            data.put("name", sysDictDatum.getDictLabel());
            data.put("value", activityService.selectAppActivityList(query).size());
            typeData.add(data);
        }

        return AjaxResult.success(typeData);
    }

    @GetMapping("/monthly-activities")
    public AjaxResult getMonthlyActivities() {
        List<Map<String, Object>> monthlyData = new ArrayList<>();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 11; i >= 0; i--) {

            DateTime date = DateUtil.offsetMonth(DateUtil.date(), -i);
            String month = DateUtil.format(date,formatter);
            Map<String, Object> data = new HashMap<>();
            data.put("month", month);
            data.put("value", activityService.list(new LambdaQueryWrapper<AppActivity>()
                    .apply("DATE_FORMAT(create_time, '%Y-%m') = {0}", month)).size());
            monthlyData.add(data);
        }
        return AjaxResult.success(monthlyData);
    }
    @GetMapping("/monthly-reg")
    public AjaxResult getMonthlyReg() {
        List<Map<String, Object>> monthlyData = new ArrayList<>();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 11; i >= 0; i--) {
            DateTime date = DateUtil.offsetMonth(DateUtil.date(), -i);
            String month = DateUtil.format(date,formatter);

            List<AppActivityReg> regs = activityRegService.list(new LambdaQueryWrapper<AppActivityReg>()
                    .apply("DATE_FORMAT(create_time, '%Y-%m') = {0}", month));

            Map<String, Object> data = new HashMap<>();
            data.put("month", month);
            data.put("value", regs.size());
            monthlyData.add(data);
        }
        return AjaxResult.success(monthlyData);

    }
    @GetMapping("/monthly-stats")
    public AjaxResult getMonthlyStats() {
        List<Map<String, Object>> monthlyData = new ArrayList<>();
        LocalDate now = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        for (int i = 11; i >= 0; i--) {
            DateTime date = DateUtil.offsetMonth(DateUtil.date(), -i);
            String month = DateUtil.format(date,formatter);


            MPJLambdaWrapper<AppActivityReg>lwq = new MPJLambdaWrapper<AppActivityReg>()
                    .selectAll(AppActivityReg.class)
                    .select(AppCreditRules::getPoints)
                    .leftJoin(AppActivity.class,AppActivity::getActivityId,AppActivityReg::getActivityId)
                    .leftJoin(AppCreditRules.class,AppCreditRules::getRuleId,AppActivity::getRuleId)
                    .apply("DATE_FORMAT(t.create_time, '%Y-%m') = {0}", month)
                    ;
                    ;
            List<AppActivityReg> dtos = appActivityRegMapper.selectJoinList(AppActivityReg.class,lwq);
            Map<String, Object> data = new HashMap<>();
            data.put("month", month);

            data.put("value", dtos.stream().map(x->x.getPoints()).reduce(BigDecimal::add).orElse(BigDecimal.ZERO));
            monthlyData.add(data);
        }
        return AjaxResult.success(monthlyData);
    }
}
